package Member_Message;

import java.io.*;

public class Test2ForMember {
    public static void main(String [] args) throws IOException, ClassNotFoundException {
        Person[] persons = new Person[10];
        for (int i = 0; i < 10; i++) {
            int member = (int)(Math.random() * 6);
            if (member == 1)
                persons[i] = new Person("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com");
            else if (member == 2)
                persons[i] = new Student("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com",(int)(Math.random() * 2)+1);
            else if (member == 3)
                persons[i] = new Employee("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com","office"+i,(int)(Math.random()*10000),(int)(Math.random()*18+2000),(int)(Math.random()*11+1),(int)(Math.random()*28));
            else if (member == 4)
                persons[i] = new Staff("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com","office"+i,(int)(Math.random()*10000),"duty"+i,(int)(Math.random()*18+2000),(int)(Math.random()*11+1),(int)(Math.random()*28));
            else if (member == 5)
                persons[i] = new Faculty("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com","office"+i,(int)(Math.random()*10000),(int)(Math.random()*18+2000),(int)(Math.random()*11+1),(int)(Math.random()*28),(int)(Math.random()*1000),"level"+i);
            else
                persons[i] = new Postgraduate("name"+i,"address"+i, (int)(Math.random()*1000000000),"email"+i+"@member.com",(int)(Math.random() * 2)+1,"direction"+i,"tutor"+i);
        }

        try(ObjectOutputStream objectOutputStream =
                    new ObjectOutputStream(new FileOutputStream("persons.dat", false))){
            objectOutputStream.writeObject(persons);
        }

        Person []newpersons = new Person[10];
        printMessage(newpersons);
    }

    private static void printMessage(Person[] persons) throws IOException, ClassNotFoundException {
        try(ObjectInputStream objectInputStream =
                    new ObjectInputStream(new FileInputStream("persons.dat"))){
            persons = (Person[])(objectInputStream.readObject());

            for (Person person : persons) {
                System.out.println(person.toString());
                System.out.println("    address:"+person.address+"    tel:"+person.telephonenumber+"    email:"+person.email);
                if (person instanceof Student){
                    Student student = (Student)person;
                    System.out.print("    grade:"+student.grade);
                    if(person instanceof Postgraduate){
                        Postgraduate postgraduate = (Postgraduate) person;
                        System.out.println("    tutor:"+postgraduate.tutor+"     direction:"+postgraduate.direction);
                    }
                    else
                        System.out.println();
                }
                else if (person instanceof Employee){
                    Employee employee = (Employee)person;
                    System.out.print("    office:"+employee.office+"      salary:"+ employee.salary+"     whenemploied:"+ employee.whenemploy);
                    if (person instanceof Faculty) {
                        Faculty faculty = (Faculty)person;
                        System.out.println("    worktime:"+faculty.worktime+"       level:"+ faculty.level);
                    }
                    else if (person instanceof Staff){
                        Staff staff = (Staff)person;
                        System.out.println("       duty:"+staff.duty);
                    }
                    else
                        System.out.println();
                }
                //System.out.println();
            }
        }
    }
}